package com.xoa.service.knowledge.impl;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.knowledge.KnowledgeColumnMapper;
import com.xoa.dao.knowledge.KnowledgeCustomerMapper;
import com.xoa.dao.knowledge.KnowledgeDocfileMapper;
import com.xoa.dao.knowledge.KnowledgeSubscribeMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.knowledge.*;
import com.xoa.model.users.Users;
import com.xoa.service.knowledge.SubscribeService;
import com.xoa.util.ExcelUtil;
import com.xoa.util.LimsJson;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SubscribeServiceImpl implements SubscribeService {
    @Autowired
    private KnowledgeSubscribeMapper knowledgeSubscribeMapper;

    @Autowired
    private KnowledgeCustomerMapper knowledgeCustomerMapper;

    @Autowired
    private KnowledgeDocfileMapper knowledgeDocfileMapper;

    @Autowired
    private KnowledgeColumnMapper knowledgeColumnMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;
    /**
     *
     * @作者 李卫卫
     * @创建日期 14:11 2020/9/14
     * @类介绍  添加客户文档订阅
     * @参数
     * @return limsJson
     */

    @Override
    public LimsJson insertSelective(HttpServletRequest request,KnowledgeSubscribe record) {
        LimsJson limsJson = new LimsJson(1, "err");
        try {
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            record.setSubscribeUser(users.getUserId());
            record.setSubscribeTime(new Date());
            KnowledgeCustomer knowledgeCustomer = knowledgeCustomerMapper.findCustomerByUserId(users.getUserId());
            if(knowledgeCustomer != null && knowledgeCustomer.getCustomerId()!=null){
                record.setCustomerId(knowledgeCustomer.getCustomerId());
            }else{
                limsJson.setMsg("您没有客户无法订阅");
                return limsJson;
            }
            record.setStatus("3");
            int c =  knowledgeSubscribeMapper.findUnWork(record);
            if(c>0){
                limsJson.setMsg("已经订阅,请等待审批");
                return limsJson;
            }
            int count = knowledgeSubscribeMapper.insertSelective(record);
            if (count > 0) {
                limsJson.setFlag(0);
                limsJson.setCode(0);
                limsJson.setMsg("订阅成功");
            } else {
                limsJson.setFlag(1);
                limsJson.setMsg("订阅失败");
            }
        } catch (Exception e) {
            limsJson.setFlag(1);
            limsJson.setMsg("添加失败，程序出现问题，请联系开发人员");
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
     *
     * @作者 李卫卫
     * @创建日期 15:11 2020/9/14
     * @类介绍  删除客户文档订阅
     * @参数
     * @return limsJson
     */

    @Override
    public LimsJson deleteByPrimaryKey(Integer subscribeId) {
        LimsJson limsJson = new LimsJson(1,"err");
        try {
                int count = knowledgeSubscribeMapper.deleteByPrimaryKey(subscribeId);
                if(count>0){
                    limsJson.setFlag(0);
                    limsJson.setCode(0);
                    limsJson.setMsg("删除成功");
                }else {
                    limsJson.setFlag(1);
                    limsJson.setMsg("删除失败");
                }

        }catch (Exception e){
            limsJson.setFlag(1);
            limsJson.setMsg("删除数据失败，程序出现问题，请联系开发人员");
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
     *
     * @作者 李卫卫
     * @创建日期 16:06 2020/9/14
     * @类介绍 编辑客户文档订阅
     * @参数
     * @return limsJson
     */

    @Override
    public LimsJson update(KnowledgeSubscribe record) {
        LimsJson limsJson = new LimsJson(1,"err");
        try {
            int count = knowledgeSubscribeMapper.updateByPrimaryKeySelective(record);
            if(count>0){
                limsJson.setFlag(0);
                limsJson.setCode(0);
                limsJson.setMsg("编辑成功");
            }else {
                limsJson.setFlag(1);
                limsJson.setMsg("编辑失败");
            }
        }catch (Exception e){
            limsJson.setFlag(1);
            limsJson.setMsg("编辑数据失败，程序出现问题，请联系开发人员");
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
     *
     * @作者 李卫卫
     * @创建日期 10:07 2020/9/15
     * @类介绍 查看订阅用户信息
     * @参数
     * @return limsJson
     */

    @Override
    public LimsJson selectByCustomerId(Integer customerId) {
        LimsJson limsJson = new LimsJson(1,"err");
        try{
            KnowledgeCustomer knowledgeCustomer = knowledgeCustomerMapper.selectByPrimaryKey(customerId);
            if(knowledgeCustomer!=null){
                limsJson.setObject(knowledgeCustomer);
                limsJson.setFlag(0);
                limsJson.setCode(0);
                limsJson.setMsg("数据查询成功");
            }else{
                limsJson.setFlag(0);
                limsJson.setMsg("查询无数据");
            }
        }catch(Exception e){
            limsJson.setFlag(1);
            limsJson.setMsg("查询数据失败，程序出现问题，请联系开发人员");
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
     *
     * @作者 李卫卫
     * @创建日期 13:56 2020/9/15
     * @类介绍
     * @参数
     * @return limsJson
     */

    @Override
    public LimsJson selectByList(HttpServletRequest request) {
        LimsJson limsJson = new LimsJson(1,"err");
        try{
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            List<KnowledgeDocfile> knowledgeDocfiles = knowledgeDocfileMapper.selectByList();
            if(knowledgeDocfiles!=null){
                for (KnowledgeDocfile k:knowledgeDocfiles) {
                    //文件等级
                    if(k.getDocfileClass() != null){
                        SysCode sysCode = sysCodeMapper.getSingleCode("KNOWLEDGE_DOCFILE_CLASS", k.getDocfileClass());
                        if(sysCode !=null && sysCode.getCodeName()!=null)
                            k.setDocfileClass(sysCode.getCodeName());

                    }
                    if(k.getColumnId()!=null){
                        String columnId = k.getColumnId();
                        String[] split = columnId.split(",");
                        if(split.length>0){
                            List<KnowledgeColumn> list  = knowledgeColumnMapper.selectByColumnIds(split);
                            if(list.size()>0){
                                String name = "";
                                for (KnowledgeColumn l :list) {
                                    name += l.getColumnName()+",";
                                }
                                if(name.endsWith(",")){
                                    name = name.substring(0,name.length()-1);
                                }
                                k.setColumnName(name);
                            }
                        }

                    }
                    if(k.getCreateUserId()!=null){
                        Users user = usersMapper.getByUserId(k.getCreateUserId());
                        if(user!=null){
                            k.setCreateUserId(user.getUserName());
                        }
                    }
                    KnowledgeSubscribe s =  knowledgeSubscribeMapper.selectByDocIdAndUserId(k.getDocfileId(),users.getUserId());
                    if(s!=null){
                        if(s.getBrowseEDate() != null ){
                            Date browseEDate = s.getBrowseEDate();
                            long end = browseEDate.getTime();
                            long now = System.currentTimeMillis();
                            if(end < now){
                                k.setBrowseStatus("1");
                            }else{
                                k.setBrowseStatus("0");
                            }
                        }else{
                            k.setBrowseStatus("1");
                        }
                        if(s.getDownloadEDate() != null ){
                            Date d = s.getDownloadEDate();
                            long end = d.getTime();
                            long now = System.currentTimeMillis();
                            if(end < now){
                                k.setDownStatus("1");
                            }else{
                                k.setDownStatus("0");
                            }
                        }else{
                            k.setDownStatus("1");
                        }

                    }else{
                        k.setBrowseStatus("1");
                        k.setDownStatus("1");
                    }

                }
                limsJson.setData(knowledgeDocfiles);
                limsJson.setFlag(0);
                limsJson.setCode(0);
                limsJson.setMsg("数据查询成功");
                limsJson.setCount(knowledgeDocfiles.size());
            }else{
                limsJson.setFlag(0);
                limsJson.setMsg("查询无数据");
            }
        }catch(Exception e){
            limsJson.setFlag(1);
            limsJson.setMsg("查询数据失败，程序出现问题，请联系开发人员");
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
     *
     * @作者 李卫卫
     * @创建日期 15:45 2020/9/16
     * @类介绍 查询订阅数据
     * @参数
     * @return limsJson
     */
    @Override
    public LimsJson getSubscription() {
        LimsJson limsJson = new LimsJson(1,"暂无数据");
        try {
            List<KnowledgeSubscribe> subscription = knowledgeSubscribeMapper.getSubscription();
            if(subscription!=null){
                for(KnowledgeSubscribe subscribe :subscription){
                    if(subscribe.getSubscribeUser()!=null){
                        Users users = usersMapper.seleById(subscribe.getSubscribeUser());
                        if(users!=null){
                            subscribe.setSubscribeUser(users.getUserName());
                        }
                    }
                }
                limsJson.setData(subscription);
                limsJson.setFlag(0);
                limsJson.setCode(0);
                limsJson.setMsg("数据查询成功");
                limsJson.setCount(subscription.size());
            }
        }catch (Exception e){
            limsJson.setFlag(1);
            limsJson.setMsg("查询数据失败，程序出现问题，请联系开发人员");
            e.printStackTrace();
        }
        return limsJson;

    }

    @Override
    public LimsJson search(HttpServletRequest request,KnowledgeDocfile knowledgeDocfile,Integer page,Integer limit) {
        LimsJson limsJson = new LimsJson(1, "err");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        try {
                if(knowledgeDocfile!=null){
                    Map paramMap = new HashMap<>();
//                    PageParams pageParams = new PageParams();
//                    if(page!=null&&limit!=null){
//                        if(page!=null&&limit!=null){ //开启分页
//                            pageParams.setUseFlag(true);
//                            pageParams.setPage(page);
//                            pageParams.setPageSize(limit);
//                            paramMap.put("page", pageParams);
//                        }
//                    }
                    //栏目类别搜索
                    if(knowledgeDocfile.getColumnId()!=null && knowledgeDocfile.getIsSysCode() != null){
                        String[] cId = knowledgeDocfile.getColumnId().split(",");
                        String[] isCode = knowledgeDocfile.getIsSysCode().split(",");
                        String cIds="";
                        String cType = "";
                        for(int i = 0 ; i<cId.length ;i++  ){
                            if(isCode[i].equals("true")){
                                cType += cId[i]+",";
                            }else{
                                cIds += cId[i]+",";
                            }
                        }
                        List<KnowledgeColumn> l =  knowledgeColumnMapper.selectByColumnTypes(cType.split(","));
                        if(l.size()>0){
                            for (KnowledgeColumn k:l) {
                                cIds += k.getColumnId()+",";
                            }
                        }
                        //去重
                        String[] split = cIds.split(",");
                        Set set = new HashSet();
                        for(int j = 0 ;j <split.length;j++){
                            set.add(split[j]);
                        }

                        String[] objects = (String[]) set.toArray(new String[set.size()]);
                        knowledgeDocfile.setColumnIds(objects);
                    }
                    if(knowledgeDocfile.getCreateUserId()!=null && !knowledgeDocfile.getCreateUserId().equals("")){
                        List<Users> byName = usersMapper.getByName(knowledgeDocfile.getCreateUserId());
                        if(byName!=null && byName.size()>0){
                            String na = "";
                            for (Users u :byName){
                                na += u.getUserId()+",";
                            }
                            if(na.length()>0){
                                knowledgeDocfile.setCreateUserIds(na.split(","));
                            }
                        }

                    }
                    paramMap.put("knowledgeDocfile",knowledgeDocfile);
                    List<KnowledgeDocfile> KnowledgeDocfileList = knowledgeDocfileMapper.search(paramMap);
                    if(KnowledgeDocfileList != null && KnowledgeDocfileList.size()>0){
                        for (KnowledgeDocfile k:KnowledgeDocfileList) {
                            k.setDownloadAddress("http://localhost:8082/knowledge/download?key="+k.getDownloadAddress());
                            if(k.getColumnId()!=null){
                                String columnId = k.getColumnId();
                                String[] split = columnId.split(",");
                                if(split.length>0){
                                    List<KnowledgeColumn> list  = knowledgeColumnMapper.selectByColumnIds(split);
                                    if(list.size()>0){
                                        String name = "";
                                        for (KnowledgeColumn l :list) {
                                            name += l.getColumnName()+",";
                                        }
                                        if(name.endsWith(",")){
                                            name = name.substring(0,name.length()-1);
                                        }
                                        k.setColumnName(name);
                                    }
                                }

                            }
                            if(k.getCreateUserId()!=null){
                                Users user = usersMapper.getByUserId(k.getCreateUserId());
                                if(user!=null){
                                    k.setCreateUserId(user.getUserName());
                                }
                            }
                            //文件等级
                            if(k.getDocfileClass() != null){
                                SysCode sysCode = sysCodeMapper.getSingleCode("KNOWLEDGE_DOCFILE_CLASS", k.getDocfileClass());
                                if(sysCode !=null && sysCode.getCodeName()!=null)
                                    k.setDocfileClass(sysCode.getCodeName());

                            }
                            KnowledgeSubscribe s =  knowledgeSubscribeMapper.selectByDocIdAndUserId(k.getDocfileId(),users.getUserId());
                            if(s!=null){
                                if(s.getBrowseEDate() != null ){
                                    Date browseEDate = s.getBrowseEDate();
                                    long end = browseEDate.getTime();
                                    long now = System.currentTimeMillis();
                                    if(end < now){
                                        k.setBrowseStatus("1");
                                    }else{
                                        k.setBrowseStatus("0");
                                    }
                                }else{
                                    k.setBrowseStatus("1");
                                }
                                if(s.getDownloadEDate() != null ){
                                    Date d = s.getDownloadEDate();
                                    long end = d.getTime();
                                    long now = System.currentTimeMillis();
                                    if(end < now){
                                        k.setDownStatus("1");
                                    }else{
                                        k.setDownStatus("0");
                                    }
                                }else{
                                    k.setDownStatus("1");
                                }

                            }else{
                                k.setBrowseStatus("1");
                                k.setDownStatus("1");
                            }
                        }
                        limsJson.setMsg("查询成功");
                        limsJson.setCode(0);
                        if(page!=null&&limit!=null){
                            limsJson.setCount(KnowledgeDocfileList.size());
                            KnowledgeDocfileList = getPageList(page,limit,KnowledgeDocfileList);
                        }
                        limsJson.setData(KnowledgeDocfileList);
                        //limsJson.setCount(pageParams.getTotal());
                        return limsJson;
                    }else{
                        limsJson.setMsg("数据为空");
                        limsJson.setCode(1);
                        return limsJson;
                    }
                }else{
                    limsJson.setMsg("请选择查询数据");
                    return limsJson;
                }
        } catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    //根据客户id查询订阅表所有该文档的订阅记录
    @Override
    public LimsJson getOldByFildAndCustomer(HttpServletRequest request ,Integer customerId,Integer fileId,Integer page,Integer limit){
        LimsJson limsJson = new LimsJson(1, "err");
        if(customerId==null||fileId==null){
            limsJson.setMsg("参数错误");
        }else{
            Map paramMap = new HashMap<>();
            PageParams pageParams = new PageParams();
            if(page!=null&&limit!=null){  //封装分页参数
                if(page!=null&&limit!=null){ //开启分页
                    pageParams.setUseFlag(true);
                    pageParams.setPage(page);
                    pageParams.setPageSize(limit);
                    paramMap.put("page", pageParams);
                }
            }
            paramMap.put("customerId",customerId);
            paramMap.put("fileId",fileId);
            List<KnowledgeSubscribe>  knowList = knowledgeSubscribeMapper.selectByCustomerIdAndFildId(paramMap);
            if(knowList!=null&&knowList.size()>0){
                for(KnowledgeSubscribe kw : knowList){
                    if(kw.getCustomerId()!=null){
                       KnowledgeCustomer customer = knowledgeCustomerMapper.selectByPrimaryKey(kw.getCustomerId());
                       if(customer!=null){
                           kw.setCustomerName(customer.getCompanyName());
                       }
                    }
                    if(kw.getDocfileId()!=null){
                        KnowledgeDocfile docfile = knowledgeDocfileMapper.selectByPrimaryKey(kw.getDocfileId());
                        if(docfile!=null){
                            kw.setDocfileName(docfile.getDocfileName());
                        }
                    }
                    if(kw.getSubscribeUser()!=null){
                        Users users = usersMapper.seleById(kw.getSubscribeUser());
                        if(users!=null){
                            kw.setSubscribeUser(users.getUserName());
                        }
                    }
                }
                limsJson.setCount(pageParams.getTotal());
                limsJson.setMsg("查询成功");
                limsJson.setCode(0);
                limsJson.setData(knowList);
            }
        }
        return limsJson;
    }


      /**
       *
       * @作者 LWW
       * @创建日期 16:38 2020/10/15
       * @类介绍  导出功能 文件管理 导出用户导出文件
       * @参数
       * @return limsJson
       */

    @Override
    public LimsJson getFileExport(HttpServletRequest request, HttpServletResponse response, KnowledgeDocfile knowledgeDocfile) {
        LimsJson limsJson = new LimsJson(1,"err");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        Map paramMap = new HashMap();
        try {
            paramMap.put("knowledgeDocfile",knowledgeDocfile);
            List<KnowledgeDocfile> KnowledgeDocfileList = knowledgeDocfileMapper.search(paramMap);
                if(KnowledgeDocfileList != null && KnowledgeDocfileList.size()>0){
                    for (KnowledgeDocfile k:KnowledgeDocfileList) {
                        if(k.getColumnId()!=null){
                            String columnId = k.getColumnId();
                            String[] split = columnId.split(",");
                            if(split.length>0){
                                List<KnowledgeColumn> list  = knowledgeColumnMapper.selectByColumnIds(split);
                                if(list.size()>0){
                                    String name = "";
                                    for (KnowledgeColumn l :list) {
                                        name += l.getColumnName()+",";
                                    }
                                    if(name.endsWith(",")){
                                        name = name.substring(0,name.length()-1);
                                    }
                                    k.setColumnName(name);
                                }
                            }
                        }
                        if(k.getCreateUserId()!=null){
                            Users user = usersMapper.getByUserId(k.getCreateUserId());
                            if(user!=null){
                                k.setCreateUserId(user.getUserName());
                            }
                        }
                        //文件等级
                        if(k.getDocfileClass() != null){
                            SysCode sysCode = sysCodeMapper.getSingleCode("KNOWLEDGE_DOCFILE_CLASS", k.getDocfileClass());
                            if(sysCode !=null && sysCode.getCodeName()!=null)
                                k.setDocfileClass(sysCode.getCodeName());
                        }
                        KnowledgeSubscribe s =  knowledgeSubscribeMapper.selectByDocIdAndUserId(k.getDocfileId(),users.getUserId());
                        if(s!=null){
                            if(s.getBrowseEDate() != null ){
                                Date browseEDate = s.getBrowseEDate();
                                long end = browseEDate.getTime();
                                long now = System.currentTimeMillis();
                                if(end < now){
                                    k.setBrowseStatus("1");
                                }else{
                                    k.setBrowseStatus("0");
                                }
                            }else{
                                k.setBrowseStatus("1");
                            }
                            if(s.getDownloadEDate() != null ){
                                Date d = s.getDownloadEDate();
                                long end = d.getTime();
                                long now = System.currentTimeMillis();
                                if(end < now){
                                    k.setDownStatus("1");
                                }else{
                                    k.setDownStatus("0");
                                }
                            }else{
                                k.setDownStatus("1");
                            }

                        }else{
                            k.setBrowseStatus("1");
                            k.setDownStatus("1");
                        }
                        if(k.getCreateTime()!=null){
                            String beginDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(k.getCreateTime());
                            k.setCreateTimeN(beginDate);
                        }
                    }
                }
            //导出excel头部标题
            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("文件管理", 6);
            String[] secondTitles = {"文档编号", "文档名称", "关键词", "文档等级", "上传人", "上传时间", "文档摘要", "知识类别"};

            //设定二级标题
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            String[] beanProperty = {"docfileNo", "docfileName", "keyWord", "docfileClass", "createUserId", "createTimeN","docfileDesc","columnName"};

            //插入数据
            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, KnowledgeDocfileList, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "文件管理.xls";

            //文件名称
            filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename=" + filename);
            workbook.write(out);//写入数据
            out.close();//关闭资源
        }catch (Exception e){
            limsJson.setFlag(1);
            limsJson.setMsg("导出数据失败，程序出现问题，请联系开发人员");
            e.printStackTrace();
        }
        return limsJson;
    }


    /**
     * 客户订阅一栏，查看所有客户所有订阅文档
     * @param customer
     * @return
     */
    @Override
    public ToJson getCustomerAndFile(HttpServletRequest request,KnowledgeCustomer customer, Integer page, Integer limit,String isSysCode) {
        ToJson json = new ToJson(1,"err");
        Map paramMap = new HashMap<>();
        //List<KnowledgeCustDocfile> resultList = new ArrayList<>(); //定义最终返回数据
        //PageParams pageParams = new PageParams();
//        if(page!=null&&limit!=null){ //开启分页
//            pageParams.setUseFlag(true);
//            pageParams.setPage(page);
//            pageParams.setPageSize(limit);
//            paramMap.put("page", pageParams);
//        }
        //栏目类别搜索
        if(customer.getColumnId()!=null && !StringUtils.checkNull(isSysCode)){
            String[] cId = customer.getColumnId().split(",");
            String[] isCode = isSysCode.split(",");
            String cIds="";
            String cType = "";
            for(int i = 0 ; i<cId.length ;i++  ){
                if(isCode[i].equals("true")){
                    cType += cId[i]+",";
                }else{
                    cIds += cId[i]+",";
                }
            }
            List<KnowledgeColumn> l =  knowledgeColumnMapper.selectByColumnTypes(cType.split(","));
            if(l.size()>0){
                for (KnowledgeColumn k:l) {
                    cIds += k.getColumnId()+",";
                }
            }
            //去重
            String[] split = cIds.split(",");
            Set set = new HashSet();
            for(int j = 0 ;j <split.length;j++){
                set.add(split[j]);
            }

            String[] objects = (String[]) set.toArray(new String[set.size()]);
            customer.setColumnIds(objects);
        }
        if(customer.getUserId()!=null && !"".equals(customer.getUserId())){
            List<Users> byName = usersMapper.getByName(customer.getUserId());
            if(byName!=null && byName.size()>0){
                String na = "";
                for (Users u :byName){
                    na += u.getUserId()+",";
                }
                if(na.length()>0){
                    customer.setCreateUserIds(na.split(","));
                }
            }

        }
        if(customer!=null){
            paramMap.put("customer",customer);
        }
        List<KnowledgeCustomer> customersList = knowledgeCustomerMapper.selectAllByParam(paramMap);
        for(KnowledgeCustomer customer1 : customersList){
            //查询该客户订阅文档
            String columnId = customer1.getColumnId();
            if(!StringUtils.checkNull(columnId)){
                String[] coluArr = columnId.split(",");
                String sysCode = "";
                for(String str : coluArr){
                    sysCode+="false,";
                }
                KnowledgeDocfile kd = new KnowledgeDocfile();
                kd.setColumnId(columnId);
                kd.setIsSysCode(sysCode);
//                LimsJson serLimsJson = search(request,kd,null,null);
//                if(serLimsJson.getCode()==0&&serLimsJson.getData()!=null&&serLimsJson.getData().size()>0){
//                    List<KnowledgeDocfile> fileList = serLimsJson.getData();
//                    for(KnowledgeDocfile kFile : fileList){
//                        KnowledgeCustDocfile kcd = new KnowledgeCustDocfile();
//                        kcd.setCompanyName(customer1.getCompanyName()); //企业名称
//                        kcd.setCompanyCode(customer1.getCompanyCode()); //机构代码
//                        if(customer1.getCompanyNatrue()!=null){ //企业性质
//                            SysCode sysCode = sysCodeMapper.getSingleCode("KNOWLEDGE_COMPANY_NATRUE",customer1.getCompanyNatrue());
//                            if(sysCode!=null){
//                                kcd.setCompanyNatrue(sysCode.getCodeName());
//                            }
//                        }
//                        kcd.setCustomerId(customer1.getCustomerId());
//                        kcd.setDocfileId(kFile.getDocfileId());
//                        kcd.setDocfileNo(kFile.getDocfileNo()); //文档编号
//                        kcd.setDocfileName(kFile.getDocfileName()); //文档名称
//                        kcd.setDocfileClass(kFile.getDocfileClass()); //文档等级
//                        kcd.setColumnName(kFile.getColumnName()); //文档知识类别
//                        kcd.setColumnId(customer1.getColumnId());
//                        //resultList.add(kcd);
//                    }
//                }
            }
            if(!StringUtils.checkNull(customer1.getColumnId())){
                String[] str = (customer1.getColumnId()).split(",");
                // 通过子节点查询该子节点之上的多有节点
                String columnName = "";
                for (String s : str) {
                    if(!StringUtils.checkNull(s)){
                        KnowledgeColumn knowledgeColumn;
                        List<String> textList = new ArrayList<>();
                        boolean flag = true;
                        knowledgeColumn = knowledgeColumnMapper.selectByPrimaryKey(Integer.valueOf(s));
                        if(knowledgeColumn!=null){
                            columnName+=knowledgeColumn.getColumnName()+",";
                        }
                    }
                }
                if(columnName.endsWith(",")){
                    columnName = columnName.substring(0,columnName.length()-1);
                }
                customer1.setColumnName(columnName);
            }
            if(customer1.getPrivType()!=null){ //权限类别
                if("0".equals(customer1.getPrivType())){
                    customer1.setbTime(customer1.getBrowseBDate());
                    customer1.seteTime(customer1.getBrowseEDate());
                    customer1.setPrivType("浏览");
                }else if("1".equals(customer1.getPrivType())){
                    customer1.setbTime(customer1.getDownloadBDate());
                    customer1.seteTime(customer1.getDownloadEDate());
                    customer1.setPrivType("下载");
                }
            }
        }
        if(customersList!=null&&customersList.size()>0){
            json.setFlag(0);
            json.setTotleNum(customersList.size());
            if(page!=null&&limit!=null){ //开启分页
                customersList = getPageList(page,limit,customersList);
                json.setData(customersList);
            }
            json.setMsg("查询成功");
        }else {
            json.setFlag(0);
            json.setMsg("暂无数据");
        }
        return json;
    }
    //业务层分页 yyl
    public List getPageList(Integer page, Integer limit, List list) {
        List list1 = new ArrayList();
        if (page == 1 && list.size() <= limit) {
            return list;
        } else if (page == 1 && list.size() > limit) {
            for (int i = 0; i < limit; i++) {
                list1.add(list.get(i));
            }
            return list1;

        } else if (page > 1) {
            if (list.size() / limit >= page) {
                for (int i = 0; i < limit; i++) {
                    list1.add(list.get(limit * (page - 1) + i));

                }
                return list1;
            } else {
                for (int i = 0; i < list.size() % 10; i++) {
                    list1.add(list.get(limit * (page - 1) + i));
                }
            }
            return list1;

        } else {
            return null;
        }
    }

}
